require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function($, common, ready, template, Collection, Model, dialog) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            this.event();
            this.fetch();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'user/orderlist',
                condition:{offset:0,limit:10},

                model: {

                },
                callback: function() {
                    loaded();
                }
            });
            //this.collection.fetch();
        },

        event: function() {
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                var model = new Model({
                    url: _global.url.api + 'user/orderdata',
                    data:{phone:$('input[name="phone"]').val()},
                    success: function (res){
                        var data = res.data;
                        $('#myorder b').html(data.mycount.id);
                        $('#myamount b').html(data.mycount.amount);
                        $('#nextorder b').html(data.nextcount.id);
                        $('#nextamount b').html(data.nextcount.amount);
                        $('#result').show();
                    }
                });
                model.post();
            });

            $('#myorder').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.phone = $('input[name="phone"]').val();
                _this.collection.condition.isNext = false;
                _this.collection.reload();
            })

            $('#nextorder').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.phone = $('input[name="phone"]').val();
                _this.collection.condition.isNext = true;
                _this.collection.reload();
            })

        }
    };
    new index();
});